package cn.edu.zjx.service.impl;

import com.management.entity.Role;
import com.management.mapper.RoleMapper;
import com.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    
    @Autowired
    private RoleMapper roleMapper;
    
    @Override
    public List<Role> getAllRoles() {
        return roleMapper.selectAll();
    }
    
    @Override
    public Role getRoleById(Integer id) {
        return roleMapper.selectById(id);
    }
    
    @Override
    @Transactional
    public boolean addRole(Role role) {
        try {
            return roleMapper.insert(role) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    @Transactional
    public boolean updateRole(Role role) {
        try {
            return roleMapper.update(role) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    @Transactional
    public boolean deleteRole(Integer id) {
        try {
            return roleMapper.deleteById(id) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public List<Role> searchRoles(String keyword) {
        return roleMapper.search(keyword);
    }
} 